//
//  DefaultPagingProcess.m
//  Pagination
//
//  Created by XMac on 2018/11/16.
//  Copyright © 2018 XMac. All rights reserved.
//

#import "DefaultPagingProcess.h"
#import "extobjc.h"

@implementation DefaultPagingProcess

@synthesize networkSuccess;

@synthesize networkFailure;

@synthesize finishRefreshWithNoMore;

@synthesize finishRefresh;

@synthesize page;

@synthesize size;

- (nonnull id)getProcessFailure {
    return ^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error){
        if (self.networkFailure) {
            NetworkFailure failure = self.networkFailure;
            failure(task,error);
            if (self.finishRefresh) {
                FinishRefresh finishRefresh = self.finishRefresh;
                finishRefresh();
            }
        }
    };
}

- (nonnull id)getProcessSuccess {
    return ^(NSURLSessionDataTask * _Nonnull task, id  _Nullable data){
        if (self.networkSuccess) {
            if ((self.page - 1) * self.size >= [data[@"total"] integerValue]) {
                if (self.finishRefreshWithNoMore) {
                    FinishRefreshWithNoMore nomore = self.finishRefreshWithNoMore;
                    nomore();
                }
            }else{
                NetworkSuccess success = self.networkSuccess;
                success(task,data);
                self.page ++;
                if (self.finishRefresh) {
                    FinishRefresh finishRefresh = self.finishRefresh;
                    finishRefresh();
                }
            }
        }
    };
}

- (void)reset:(int)defaultPage {
    self.page = defaultPage;
}


-(void)setNetworkStatusBlockWithSuccess:(NetworkSuccess)networkSuccess
                            withFailure:(NetworkFailure)networkFailure
                      withUIFinishBlock:(FinishRefresh)finishRefresh
                             withNoMore:(FinishRefreshWithNoMore)finishRefreshWithNoMore{
    self.networkSuccess = networkSuccess;
    self.networkFailure = networkFailure;
    self.finishRefresh = finishRefresh;
    self.finishRefreshWithNoMore = finishRefreshWithNoMore;
}









@end
